package com.woniuxy.huayoung.service.impl;

import com.woniuxy.huayoung.entity.*;
import com.woniuxy.huayoung.mapper.RolePermMapper;
import com.woniuxy.huayoung.mapper.UserMapper;
import com.woniuxy.huayoung.mapper.UserMessageMapper;
import com.woniuxy.huayoung.mapper.UserRoleMapper;
import com.woniuxy.huayoung.service.UserService;
import com.woniuxy.huayoung.utils.JwtUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.File;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private UserMessageMapper userMessageMapper;
    @Resource
    private UserRoleMapper userRoleMapper;
    @Resource
    private RolePermMapper rolePermMapper;

    @Override
    public User login(String telephone) {
        return userMapper.login(telephone);
    }

    @Transactional
    @Override
    public boolean regist(String telephone, String pwd) {

        return userMapper.regist(telephone,pwd);

    }

    //查询关注的用户
    @Override
    public Attention findAttentions(String token) {
        String telephone = JwtUtil.getAccount(token);
        int id = userMapper.findIdByTelephone(telephone);
        return userMapper.findAttentions(id);
    }

    //修改个人信息
    @Transactional
    @Override
    public int updateUser(User user) {
        return userMapper.updateUser(user);
    }


    //通过电话token的电话查询个人详细信息
    @Override
    public User findByTelephone(String telephone) {
        return userMapper.findByTelephone(telephone);
    }

    @Override
    public User findById(int id) {
        return userMapper.findById(id);
    }



    @Transactional
    @Override
    public Attention cancelAttention(int attentionedUid, int attentionUid) {
        int i = userMapper.cancelAttention(attentionedUid, attentionUid);
        if(i != 0){
            //如果删除成功，重新查询关注的对象再返回
            Attention attentions = userMapper.findAttentions(attentionUid);
            return attentions;
        }
        return new Attention();
    }

    @Transactional
    @Override
    public int updateHeadPortrait(String img, String telephone) {
        //获取当前的头像图片名
        String image = this.findHeadPortrait(telephone);
        File file = new File("D:\\huayoung\\headportrait\\"+image);
        if(file.exists()){
            file.delete();
        }
        return userMapper.updateHeadPortrait(img, telephone);
    }

    @Override
    public String findHeadPortrait(String telephone) {
        return userMapper.findHeadPortrait(telephone);
    }

    @Override
    public int findIdByToken(String token) {
        //解析出telephone
        String telephone = JwtUtil.getAccount(token);
        return userMapper.findIdByTelephone(telephone);
    }


    @Override
    @Transactional
    public void insertUser(User user) {
        user.setAccount("张三");
        userMapper.insertUser(user);
        int userId = userMapper.findIdByTelephone(user.getTelephone());
        UserMessage userMessage = new UserMessage();
        userMessage.setTelephone(user.getTelephone());
        userMessage.setUid(userId);

        userMessage.setDel(0);
        userMessage.setLevel(1);
        userMessage.setImage(null);
        userMessage.setIsVip(0);
        userMessageMapper.insertUserMessage(userMessage);
        //
        UserRole userRole = new UserRole(userId, 2);
        userRoleMapper.insertOne(userRole);

        //
        rolePermMapper.insertOne(new RolePerm(2, 2, userId));
        rolePermMapper.insertOne(new RolePerm(2, 3, userId));

    }

    //
    @Override
    public List<User> findAll() {
        return userMapper.findAll();
    }

    //查询所有管理员
    @Override
    public List<User> findAllAdminbyRid() {
        return userMapper.findAllAdminbyRid();
    }

    //逻辑删除管理员，修改del状态
    @Override
    public Boolean delAdmin(Integer id) {
        return userMapper.delAdmin(id);
    }

    //修改管理员信息
    @Override
    public int updateAdmin(User user) {
        return userMapper.updateAdmin(user);
    }

    @Override
    public List<Perm> findAllPerms() {
        return userMapper.findAllPerms();
    }
}
